﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;

namespace ConvertOffice
{
    /*
     * 注：为引用dll，在属性中将“嵌入互操作类型”改为：Flase
     */
    public class PPT2Wmv
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="targetPath">目标文件路径（不包含扩展名）</param>
        public void Excute(string srcPath, string targetPath)
        {
            ApplicationClass app = null;
            Presentations pres = null;
            Presentation file = null;

            try
            {
                app = new ApplicationClass();
                pres = app.Presentations;
                file = pres.Open(srcPath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

                // 转wmv
                file.CreateVideo(targetPath);
                while (true)
                {
                    //Thread.Sleep(100);
                    // 成功和失败就退出
                    if (file.CreateVideoStatus == PpMediaTaskStatus.ppMediaTaskStatusDone || file.CreateVideoStatus == PpMediaTaskStatus.ppMediaTaskStatusFailed)
                        break;
                }
            }
            finally
            {
                Marshal.ReleaseComObject(file);
                file = null;
                Marshal.ReleaseComObject(pres);
                pres = null;
                if (app != null)
                {
                    app.Quit();
                    Marshal.ReleaseComObject(app);
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }
}
